package com.future.future.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author: shinan
 * @Version: 1.8
 * @create: 2020/12/28 11:39
 * @Description: 为网关配置跨域信息
 */
@Configuration
public class FutureCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {

        //导入org.springframework.web.cors.reactive下的  基于响应式的
        //基础的跨域配置源
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        //跨域配置信息
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨域  允许任意响应头
        corsConfiguration.addAllowedHeader("*");
        //2、 允许任意请求方式
        corsConfiguration.addAllowedMethod("*");
        //3、允许任意源
        corsConfiguration.addAllowedOrigin("*");
        //4、是否支持用户凭据
        corsConfiguration.setAllowCredentials(true);

        //5、未任意路径配置跨域
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}